package com.mazaiting.manager.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mazaiting.common.core.ex.exceptions.BaseException;
import com.mazaiting.common.core.ex.exceptions.BusinessException;
import com.mazaiting.common.core.ex.exceptions.EmptyObjectException;
import com.mazaiting.manager.domain.form.DictTypeForm;
import com.mazaiting.manager.domain.query.DictTypePageQuery;
import com.mazaiting.manager.domain.vo.dict.DictTypePageVO;
import com.mazaiting.manager.service.ISysDictTypeService;
import com.mazaiting.mybatisplus.domain.vo.BasePageVO;
import com.mazaiting.web.auth.security.annotation.PrePermission;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;

/**
 * 字典控制器
 *
 * @author mazaiitngg
 * @since 2022-03-25
 */
@Slf4j
@RestController
@RequestMapping("/manager/v1/dictType")
@RequiredArgsConstructor
public class SysDictTypeController {

    private final ISysDictTypeService sysDictTypeService;

    /**
     * 字典类型分页列表
     * @param queryParams 查询参数
     * @return 分页数据
     */
    @PrePermission("sys:dict:type:query")
    @GetMapping
    public BasePageVO<DictTypePageVO> listDictTypePages(DictTypePageQuery queryParams) {
        Page<DictTypePageVO> result = sysDictTypeService.listPages(queryParams);
        return BasePageVO.success(result);
    }

    /**
     * 新增字典类型
     * @param dictTypeForm 字典表单
     * @return true: 新增成功; false: 新增失败
     */
    @PrePermission("sys:dict:type:add")
    @PostMapping
    public boolean saveDictType(DictTypeForm dictTypeForm) throws BusinessException {
        return sysDictTypeService.save(dictTypeForm);
    }

    /**
     * 修改字典类型
     * @param dictTypeForm 字典表单类型
     * @return true: 修改成功; false: 修改失败
     */
    @PrePermission("sys:dict:type:update")
    @PutMapping
    public boolean updateDict(DictTypeForm dictTypeForm) throws BaseException {
        return sysDictTypeService.update(dictTypeForm);
    }

    /**
     * 删除字典类型
     * @param ids 字典 ID. 字典类型ID，多个以英文逗号(,)分割
     * @return true: 删除成功; false: 删除失败
     */
    @PrePermission("sys:dict:type:delete")
    @DeleteMapping("/{ids}")
    public boolean deleteDictTypes(@NotEmpty(message = "删除数据为空") @PathVariable String ids) {
        return sysDictTypeService.delete(ids);
    }

    /**
     * 字典类型表单详情
     * @param id 字典ID
     * @return 表单详情
     */
    @PrePermission("sys:dict:type:query")
    @GetMapping("/{id}/formData")
    public DictTypeForm getDictTypeFormData(@PathVariable Long id) throws EmptyObjectException {
        return sysDictTypeService.getDictTypeFormData(id);
    }
}
